package qc.module.cms.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.CmsCategoryTypeEnum;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.cms.dto.category.CategoryAddDto;
import qc.module.cms.dto.category.CategoryDto;
import qc.module.cms.dto.category.CategorySimpleDto;
import qc.module.cms.dto.category.CategoryTreeTableDetailDto;
import qc.module.cms.entity.Category;

import java.util.List;

/**
 * CategoryMapper
 *
 * @author QuCheng Tech
 * @since 2023/5/26
 */
@Mapper
public interface CategoryMapper {
    CategoryMapper MAPPER = Mappers.getMapper(CategoryMapper.class);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "pid", ignore = true)
    @Mapping(target = "odr", ignore = true)
    @Mapping(target = "flag", ignore = true)
    Category toEntity(CategoryDto dto);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "pid", ignore = true)
    @Mapping(target = "odr", ignore = true)
    @Mapping(target = "flag", ignore = true)
    Category toEntity(CategoryAddDto dto);


    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "level", ignore = true)
    @Mapping(target = "deptauth", source = "deptauth")
    @Mapping(target = "candraft", source = "candraft")
    CategoryDto toDto(Category en);

    @Mapping(target = "level", ignore = true)
    @Mapping(target = "deptauth", source = "deptauth")
    @Mapping(target = "candraft", source = "candraft")
    CategorySimpleDto toSimpleDto(Category en);

    @IterableMapping(qualifiedByName = "CategoryEntityToDto")
    List<CategoryDto> toDtoList(List<Category> list);
    
    @IterableMapping(qualifiedByName = "CategoryEntityToSimpleDto")
    List<CategorySimpleDto> toSimpleDtoList(List<Category> list);


    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "level", ignore = true)
    @Mapping(target = "children", ignore = true)
    CategoryTreeTableDetailDto toTreeTableDetailDto(Category en);

    @IterableMapping(qualifiedByName = "EntityToTreeTableDetailDto")
    List<CategoryTreeTableDetailDto> toTreeTableDetailDtoList(List<Category> list);


    /**
     * Entity转换为栏目Dto
     *
     * @param category:
     * @return CategoryDto
     * @author QcCheng Tech
     * @date 2023/8/1
     */
    @Named("EntityToTreeTableDetailDto")
    default CategoryTreeTableDetailDto EntityToTreeTableDetailDto(Category category) {
        int odr = Integer.parseInt(category.getOdr());
        if (odr < 0 || odr > 99) {
            category.setOdr(null);
        }
        // Entity转换Dto
        CategoryTreeTableDetailDto categoryTreeTableDetailDto = CategoryMapper.MAPPER.toTreeTableDetailDto(category);
        return categoryTreeTableDetailDto;
    }

    /**
     * 新增栏目DTO转换为Entity
     *
     * @param dto 新增栏目DTO
     * @return 栏目Entity
     * @author QuCheng Tech
     * @since 2023/5/27
     */
    default Category CategoryAddDtoToEntity(CategoryAddDto dto) {
        //DTO转换为Entity
        Category en = CategoryMapper.MAPPER.toEntity(dto);
        //父级ID转换
        //修改DTO中使用int类型，判断是否大于0，大于0表示有效
        if (dto.getPid() > 0x0) {
            en.setPid(dto.getPid());
        } else {
            en.setPid(null);
        }
        //判断栏目排序号可以为空或传入的不是数值；后台控制有效值在1~99，输入无效时设置数据库中为100
        if (StringUtils.isBlank(dto.getOdr())) {
            en.setOdr("100");
        } else {
            try {
                int order = Integer.parseInt(dto.getOdr());
                if (order < 1 || order > 100) {
                    en.setOdr("100");
                } else {
                    en.setOdr(String.valueOf(order));
                }
            } catch (NumberFormatException e) {
                en.setOdr("100"); //输入无效值设置数据库中为100
            }
        }
        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (!StringUtils.isBlank(dto.getFlag())) {
            try {
                flag = Integer.parseInt(dto.getFlag());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    /**
     * Entity转换为栏目Dto
     *
     * @param category:
     * @return CategoryDto
     * @author QcCheng Tech
     * @date 2023/8/1
     */
    @Named("CategoryEntityToDto")
    default CategoryDto CategoryEntityToDto(Category category) {
        int odr = Integer.parseInt(category.getOdr());
        if (odr < 0 || odr > 99) {
            category.setOdr(null);
        }
        // Entity转换Dto
        CategoryDto categoryDto = CategoryMapper.MAPPER.toDto(category);
        return categoryDto;
    }

    @Named("CategoryEntityToSimpleDto")
    default CategorySimpleDto CategoryEntityToSimpleDto(Category category) {
        // Entity转换simpleDto
        CategorySimpleDto categorysimpleDto = toSimpleDto(category);
        return categorysimpleDto;
    }

    /**
     * 修改栏目DTO转换为Entity
     *
     * @param dto 新增栏目DTO
     * @return 栏目Entity
     * @author QuCheng Tech
     * @since 2023/5/27
     */
    default Category CategoryDtoToEntity(CategoryDto dto) {
        //DTO转换为Entity
        Category en = CategoryMapper.MAPPER.toEntity(dto);

        if (dto.getId() > 0x0)
            en.setId(dto.getId());

        //父级ID转换
        if (dto.getPid() > 0x0) {
            en.setPid(dto.getPid());
        } else {
            en.setPid(null);
        }

        //判断栏目排序号可以为空或传入的不是数值；后台控制有效值在1~99，输入无效时设置数据库中为100
        if (StringUtils.isBlank(dto.getOdr())) {
            en.setOdr("100");
        } else {
            try {
                int order = Integer.parseInt(dto.getOdr());
                if (order < 1 || order > 100) {
                    en.setOdr("100");
                } else {
                    en.setOdr(String.valueOf(order));
                }
            } catch (NumberFormatException e) {
                en.setOdr("100"); //输入无效值设置数据库中为100
            }
        }
        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (dto.getFlag() > 0x0) {
            try {
                flag = dto.getFlag();
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    static CmsCategoryTypeEnum getCmsCategoryTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (CmsCategoryTypeEnum item : CmsCategoryTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return CmsCategoryTypeEnum.DEFAULT;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }
}
